package com.ctshk.rpc.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.rpc.custom.entity.CustomizedTripScheduleAmountStatistics;
import com.ctshk.rpc.custom.mapper.CustomizedTripScheduleAmountStatisticsMapper;
import com.ctshk.rpc.custom.req.trip.TripScheduleAmountStatisticsReq;
import com.ctshk.rpc.custom.service.ICustomizedTripScheduleAmountStatisticsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 团期金额统计 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-12
 */
@DubboService
public class CustomizedTripScheduleAmountStatisticsServiceImpl extends ServiceImpl<CustomizedTripScheduleAmountStatisticsMapper, CustomizedTripScheduleAmountStatistics> implements ICustomizedTripScheduleAmountStatisticsService {

    @Autowired
    private CustomizedTripScheduleAmountStatisticsMapper tripScheduleAmountStatisticsMapper;



    /**
     * 更新团期金额统计
     * @param req
     * @return
     */
    @Override
    public Integer updateAmountStatistics(TripScheduleAmountStatisticsReq req) {
        QueryWrapper<CustomizedTripScheduleAmountStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id",req.getScheduleId());
        queryWrapper.eq("is_deleted",0);
        CustomizedTripScheduleAmountStatistics amountStatistics = tripScheduleAmountStatisticsMapper.selectOne(queryWrapper);
        if (amountStatistics != null){
            if (req.getType().intValue() == NumberConstant.ONE.getValue()){
                //新增
                amountStatistics.setGroupFeeOrder(amountStatistics.getGroupFeeOrder().add(req.getAddOrderAmount()));
                amountStatistics.setOrderPeople(amountStatistics.getOrderPeople()+req.getAddOrderPeople());
            }else{
                //减少
                amountStatistics.setGroupFeeOrder(amountStatistics.getGroupFeeOrder().subtract(req.getAddOrderAmount()));
                amountStatistics.setOrderPeople(amountStatistics.getOrderPeople()-req.getAddOrderPeople());
            }
            amountStatistics.setModifiedId(req.getUserId());
            return tripScheduleAmountStatisticsMapper.updateById(amountStatistics);
        }
        return 0;
    }

    /**
     * 团期数据回滚
     *
     * @param scheduleId
     * @param totalPeople
     * @param totalTourFee
     * @param userId
     * @return
     */
    @Override
    public boolean rollBackStatistics(Long scheduleId,Integer totalPeople, BigDecimal totalTourFee,Long userId) {
        QueryWrapper<CustomizedTripScheduleAmountStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id",scheduleId);
        queryWrapper.eq("is_deleted",0);
        CustomizedTripScheduleAmountStatistics statistics = tripScheduleAmountStatisticsMapper.selectOne(queryWrapper);
        if (statistics != null){
            statistics.setGroupFeeOrder(statistics.getGroupFeeOrder().subtract(totalTourFee));
            statistics.setOrderPeople(statistics.getOrderPeople() - totalPeople);
            statistics.setModifiedId(userId);
            //更新统计
            return updateById(statistics);
        }
        return true;
    }
}
